#include "syntaxhighlighter.hh"



SyntaxHighlighter::SyntaxHighlighter(QTextDocument* document) : QSyntaxHighlighter(document)
{
    // Section
    QTextCharFormat sectionFormat;
    sectionFormat.setFontWeight(QFont::Bold);
    sectionFormat.setForeground(Qt::darkCyan);
    m_rules.append(Rule(QRegExp("\\.[a-zA-Z0-9]+"), sectionFormat));

    // Label
    QTextCharFormat labelFormat;
    labelFormat.setFontWeight(QFont::Bold);
    labelFormat.setForeground(Qt::darkMagenta);
    m_rules.append(Rule(QRegExp("[a-zA-Z0-9]+:"), labelFormat));

    // Quoted Strings
    QTextCharFormat stringFormat;
    stringFormat.setForeground(Qt::darkRed);
    m_rules.append(Rule(QRegExp("\".*\""), stringFormat));

    // Comments
    QTextCharFormat commentFormat;
    commentFormat.setFontItalic(true);
    commentFormat.setForeground(Qt::darkGreen);
    m_rules.append(Rule(QRegExp(";.*"), commentFormat));


}

void SyntaxHighlighter::highlightBlock(const QString &text)
{
    foreach(Rule rule, m_rules)
    {
        int index = text.indexOf(rule.expression);
        while (index >= 0) {
            int length = rule.expression.matchedLength();
            setFormat(index, length, rule.format);
            index = text.indexOf(rule.expression, index + length);
        }
    }


   /* QTextCharFormat myClassFormat;
    myClassFormat.setFontItalic(true);
    myClassFormat.setForeground(Qt::darkGreen);
    QString pattern = ";.*";

    QRegExp expression(pattern);
    int index = text.indexOf(expression);
    while (index >= 0) {
        int length = expression.matchedLength();
        setFormat(index, length, myClassFormat);
        index = text.indexOf(expression, index + length);
    }*/
}
